package com.trade.module.base.repository;

import com.trade.module.base.entity.UserRole;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

/**
 * Class Description: 用戶角色數據持久層
 *
 * @Version v1.0 2017/4/22 21:04
 * @JDK version 1.8.0_51
 * @Author zfb
 */
@Repository
public interface UserRoleRepository extends PagingAndSortingRepository<UserRole,String>,
        JpaSpecificationExecutor<UserRole>{

    /**
     * 根據用戶id和數據狀態查詢UserRole集合
     *
     * @param userId 用戶id
     * @param userRoleDataFlag 數據刪除狀態
     * @param userDataFlag 數據刪除狀態
     * @return Iterable<UserRole>
     */
    @Query(value = "FROM UserRole ur WHERE ur.dataFlag=?2 AND ur.userId IN" +
            " (SELECT u.id from User u where u.id=?1 and u.dataFlag=?3) " +
            "order by ur.createTime ASC")
    Iterable<UserRole> findUserRoleByUserId(String userId,
                                            String userRoleDataFlag,
                                            String userDataFlag);
}
